package com.lagou.base;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数，要访问的方法名
        //String methodName = req.getParameter("methodName");
        String methodName=null;
        //1.获取post请求得content-type类型
        String contentType = req.getHeader("Content-Type");
        if("application/json;charset=utf-8".equals(contentType)){
            //是JOSN格式 调用getPostJSON
            String postJSON=getPostJSON(req);

            //将JSON格式得字符串 转化为MAP
            Map<String,Object> map = JSON.parseObject(postJSON, Map.class);
            //从map集合中获取methodName
            methodName=(String )map.get("methodName");
            //将获取到得数据，保存到request域对象中
            req.setAttribute("map",map);
        }else{
            methodName=req.getParameter("methodName");
        }
        //2.判断 执行对应的方法
        if(methodName!=null){
            try {
                //通过反射优化代码 提升代码的可维护性
                //1.通过字节码文件对象
                Class c = this.getClass();
                //2.根据传入的方法名，获取对应的方法对象
                Method method = c.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
                //3.调用method对象的invoke方法，执行对应的功能
                method.invoke(this,req,resp);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("请求功能不存在！");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    /**
     * POST请求格式为 application/json;charset=utf-8
     * 使用该方法进行读取
     */

    public String getPostJSON(HttpServletRequest request){
        try {
            //1.从request中 获取缓冲输入流对象
            BufferedReader reader = request.getReader();
            //2.创建StringBuffer 保存读取出的数据
            StringBuffer sb=new StringBuffer();
            //3.循环读取
            String line=null;
            while((line=reader.readLine())!=null){
                sb.append(line);
            }
            //4.返回结果
            return  sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
